package cn.g31010.sip.service
{
	import com.adobe.cairngorm.business.ServiceLocator;
	
	import mx.rpc.AbstractOperation;
	import mx.rpc.AbstractService;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	public class HessianClient implements IResponder
	{
		private var resultCallbackFunction : Function;   
	    private var faultCallbackFunction : Function;
	    private var service:AbstractService; 
	    
	    public function HessianClient(destination:String,resultCallback:Function=null, faultCallback:Function=null)
		{
			this.service =ServiceLocator.getInstance().getService(destination);
			if (resultCallback == null) {   
             resultCallbackFunction = defaultFunction;   
         } else {   
             resultCallbackFunction = resultCallback;   
         }   
         if (faultCallback == null) {   
             faultCallbackFunction = defaultFunction;   
         } else {   
             faultCallbackFunction = faultCallback;   
         }
		}
		
		public function request(operation:String,... args:Array):void{
			var aop:AbstractOperation=service.getOperation(operation);
			aop.arguments=args;
			var token:AsyncToken=aop.send();
			token.addResponder(this);
		}

		 public function result(data:Object):void { 
		 	var event:ResultEvent = data as ResultEvent;
         	resultCallbackFunction(event.result);  
     	}   
        
     	public function fault(data:Object):void {   
     		var event:FaultEvent = data as FaultEvent;
         	faultCallbackFunction(event);   
         	
     	}   
        
     	public function defaultFunction(data:Object):void {
     	} 
	}
}